package com.totoro.mongo.gridfs;

import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.totoro.mongo.MongoUtil;
import org.bson.types.ObjectId;

import java.io.*;

/**
 * Created by Niki on 2018/3/28 17:43
 */
public class GridFSDemo {
    private static final String DATABASE_NAME = "gridfs";


    private static GridFS createGridFs(String dataBaseName) {
        MongoClient client = MongoUtil.getClient();
        MongoDatabase database = client.getDatabase(DATABASE_NAME);
        DB db = client.getDB(DATABASE_NAME);
        GridFS gridFS = new GridFS(db);
        return gridFS;
    }

    private static GridFS createGridFs(MongoClient client,String dataBaseName){
        MongoDatabase database = client.getDatabase(DATABASE_NAME);
        DB db = client.getDB(DATABASE_NAME);
        GridFS gridFS = new GridFS(db);
        return gridFS;
    }

    /**
     * 读取文件
     *
     * @param gridFS
     * @param key
     * @param filePath
     * @throws IOException
     */
    private static void readFile(GridFS gridFS, String key, String filePath) throws IOException {
        GridFSDBFile file = gridFS.find(new ObjectId(key));

        file.writeTo(new File(filePath));
    }

    private void readFileByStream(GridFS gridFS, String key, String filePath) throws IOException {
        GridFSDBFile file = gridFS.find(new ObjectId(key));

        BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream(), 1024);
        OutputStream outputStream = new FileOutputStream(new File(filePath));
        byte[] bytes = new byte[1024];
        int count = 0;
        while ((count = inputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, count);
        }

        while (true) {
            int num = inputStream.read(bytes);
            if (num > 0) {
                outputStream.write(bytes, 0, num);
            }
            if (num < 1024) {
                break;
            }
        }
    }


    /**
     * 删除文件
     *
     * @param gridFS
     * @param fileName
     */
    private static void deleteFile(GridFS gridFS, String fileName) {
        gridFS.remove(fileName);
    }

    /**
     * 上传文件
     * @param client
     * @param filepath
     * @return
     * @throws FileNotFoundException
     */
    private static String uploadFileByStream(MongoClient client, String filepath) throws FileNotFoundException {
        File file = new File(filepath);
        FileInputStream inputStream = new FileInputStream(file);

        MongoDatabase database = MongoUtil.getDataBase(client, DATABASE_NAME);

        GridFSBucket bucket = GridFSBuckets.create(database);
        ObjectId fileId = bucket.uploadFromStream(file.getName(), inputStream);

        return fileId.toHexString();
    }

    /**
     * 上传文件
     *
     * @param gridFS
     * @param filePath
     * @throws IOException
     */
    public static void uploadFile(GridFS gridFS, String filePath) throws IOException {
        gridFS.createFile(new File(filePath));
    }

    public static void main(String[] args) throws IOException {
        MongoClient client = MongoUtil.getClient();
        GridFS gridFS = createGridFs(client, DATABASE_NAME);

        String filePath = "D:/demo.csv";
        String fileId = uploadFileByStream(client, filePath);
        System.out.println(fileId);
//        deleteFile(gridFS, "demo.csv");

        DBCursor cursor = gridFS.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
}
